﻿namespace CSharpTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            
            int width = 50;
            int height = 30;
            Console.SetWindowSize(width, height);
            Console.SetBufferSize(width, height);
            Console.CursorVisible = false;

            int stateId = 1;

            string endWords = "";

            while (true)
            {
                switch (stateId)
                {
                    case 1:
                        Console.Clear();

                        Console.SetCursorPosition(width / 2 - 4, 2);
                        Console.Write("营救公主");

                        Console.SetCursorPosition(width / 2 - 4, 5);
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("游戏开始");

                        Console.SetCursorPosition(width / 2 - 4, 7);
                        Console.ForegroundColor =ConsoleColor.White;
                        Console.Write("退出游戏");


                        int choice = 0;

                        while (true)
                        {
                            char op = Console.ReadKey(true).KeyChar;
                            bool startGame = false; 
                            switch (op)
                            {
                                case 'w':
                                case 'W':
                                    choice--;
                                    choice = Math.Max(0,choice);
                                    break;
                                case 's':
                                case 'S':
                                    choice++;
                                    choice = Math.Min(1, choice);
                                    break;
                                case 'j':
                                case 'J':
                                    if (choice == 0)
                                    {
                                        startGame = true;
                                    }
                                    else if (choice == 1)
                                    {
                                        Environment.Exit(0);
                                    }
                                    break;
                                default:break;
                            }

                            if (startGame)
                            {
                                stateId = 2;
                                break;
                            }
                            Console.SetCursorPosition(width / 2 - 4, 5);
                            Console.ForegroundColor = choice == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("游戏开始");

                            Console.SetCursorPosition(width / 2 - 4, 7);
                            Console.ForegroundColor = choice == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("退出游戏");
                        }
                        break;
                    case 2:
                        
                        #region 环境
                        for (int i = 0; i < width; i += 2)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            //上
                            Console.SetCursorPosition(i, 0);
                            Console.Write("■");

                            //中
                            Console.SetCursorPosition(i, height-6);
                            Console.Write("■");

                            //下
                            Console.SetCursorPosition(i, height-1);
                            Console.Write("■");
                        }

                        for (int i = 0; i < height; i++ )
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            //左
                            Console.SetCursorPosition(0, i);
                            Console.Write("■");

                            //右
                            Console.SetCursorPosition(width-2, i);
                            Console.Write("■");
                        }
                        #endregion

                        #region Boss设置
                        string bossIcon = "■";
                        int bossMinAtk = 7;
                        int bossMaxAtk = 13;
                        int bossHp = 100;
                        ConsoleColor bossColor = ConsoleColor.Green;
                        int bossPosX = 24;
                        int bossPosY = 15;
                        #endregion

                        #region Player设置
                        string playerIcon = "●";
                        int playerMinAtk = 8;
                        int playerMaxAtk = 12;
                        int playerHp = 100;
                        ConsoleColor playerColor = ConsoleColor.Yellow;
                        int playerPosX = 20;
                        int playerPosY = 15;
                        Console.ForegroundColor = playerColor;
                        Console.SetCursorPosition(playerPosX, playerPosY);
                        Console.Write(playerIcon);
                        #endregion


                        #region princess设置
                        string princessIcon = "◆";
                        int princessPosX = 24;
                        int princessPosY = 5;
                        ConsoleColor princessColor = ConsoleColor.Blue;
                        #endregion

                        char playerInput;
                        bool isFighting = false;
                        bool isOver = false;

                        while (true)
                        {
                            if (isFighting)
                            {
                                playerInput = Console.ReadKey(true).KeyChar;
                                if (playerInput == 'j' || playerInput == 'J')
                                {
                                    if (playerHp <= 0)
                                    {
                                        stateId = 3;
                                        endWords = "营救失败";
                                        break;
                                    }
                                    else if (bossHp <= 0)
                                    {
                                        isFighting = false;
                                        Console.SetCursorPosition(bossPosX, bossPosY);
                                        Console.Write("  ");

                                        Console.SetCursorPosition(2, height - 5);
                                        Console.Write("                                          ");
                                        Console.SetCursorPosition(2, height - 5);
                                        Console.ForegroundColor = ConsoleColor.Yellow;
                                        Console.Write("快前往 公主身边 营救 公主 吧！");

                                        Console.SetCursorPosition(2, height - 4);
                                        Console.Write("                                          ");
                      

                                        Console.SetCursorPosition(2, height - 3);
                                        Console.Write("                                          ");
                                        Console.SetCursorPosition(2, height - 3);
                                        Console.ForegroundColor = ConsoleColor.Yellow;
                                        Console.Write("按J键营救");

                                    }
                                    else
                                    {
                                        Random r = new Random();
                                        int bossAtk = r.Next(bossMinAtk, bossMaxAtk+1);
                                        playerHp -= bossAtk;
                                        playerHp = Math.Clamp(playerHp, 0, 100);

                                        if (bossHp > 0)
                                        {
                                            int playerAtk = r.Next(playerMinAtk, playerMaxAtk+1);
                                            bossHp -= playerAtk;
                                            bossHp = Math.Clamp(bossHp, 0, 100);

                                            Console.SetCursorPosition(2, height - 5);
                                            Console.Write("                                          ");
                                            Console.SetCursorPosition(2, height - 5);
                                            Console.Write("战斗进行中，按J键继续");

                                            Console.SetCursorPosition(2, height - 4);
                                            Console.Write("                                          ");
                                            Console.SetCursorPosition(2, height - 4);
                                            Console.ForegroundColor = ConsoleColor.Yellow;
                                            Console.Write("Boss对玩家造成{0}伤害,玩家剩余血量为{1}",bossAtk,playerHp);

                                            Console.SetCursorPosition(2, height - 3);
                                            Console.Write("                                          ");
                                            Console.SetCursorPosition(2, height - 3);
                                            Console.ForegroundColor = ConsoleColor.Green;
                                            Console.Write($"玩家对Boss造成{playerAtk}伤害,Boss剩余血量为{bossHp}");

                                            if (playerHp <= 0)
                                            {
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.Write("                                          ");
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.ForegroundColor = ConsoleColor.Red;
                                                Console.Write("玩家未能通过Boss的考验");

                                                Console.SetCursorPosition(2, height - 4);
                                                Console.Write("                                          ");
                                                Console.SetCursorPosition(2, height - 4);
                                                Console.ForegroundColor = ConsoleColor.Yellow;
                                                Console.Write("路途遥远,少侠重新来过");

                                                Console.SetCursorPosition(2, height - 3);
                                                Console.Write("                                          ");
                                                Console.SetCursorPosition(2, height - 3);
                                                Console.ForegroundColor = ConsoleColor.Yellow;
                                                Console.Write("按J键继续");
                                            }
                                            else if (bossHp <= 0)
                                            {
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.Write("                                          ");
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.ForegroundColor = ConsoleColor.Red;
                                                Console.Write("你成功 击败 BOSS 了！");

                                                Console.SetCursorPosition(2, height - 4);
                                                Console.Write("                                          ");
                                                Console.SetCursorPosition(2, height - 4);
                                                Console.ForegroundColor = ConsoleColor.Yellow;
                                                Console.Write("按J键继续");

                                                Console.SetCursorPosition(2, height - 3);
                                                Console.Write("                                          ");
                          
                                            }

                                        }

                                    }
                                }
                            }
                            else
                            {
                                if (bossHp > 0)
                                {
                                    Console.ForegroundColor = bossColor;
                                    Console.SetCursorPosition(bossPosX, bossPosY);
                                    Console.Write(bossIcon);
                                }
                                else
                                {
                                    Console.ForegroundColor =princessColor;
                                    Console.SetCursorPosition(princessPosX, princessPosY);
                                    Console.Write(princessIcon);
                                }

                                if (playerHp > 0)
                                {
                                    playerInput = Console.ReadKey(true).KeyChar;
                                    Console.SetCursorPosition(playerPosX, playerPosY);
                                    Console.Write("  ");

                                    switch (playerInput)
                                    {
                                        case 'w':
                                        case 'W':
                                            playerPosY--;
                                            if (playerPosY < 1)
                                            {
                                                playerPosY = 1;
                                            }
                                            else if (playerPosX == bossPosX && playerPosY == bossPosY && bossHp > 0)
                                            {
                                                playerPosY++;
                                            }
                                            else if (playerPosX == princessPosX && playerPosY == princessPosY && bossHp <= 0)
                                            {
                                                playerPosY++;
                                            }
                                            break;

                                        case 's':
                                        case 'S':
                                            playerPosY++;
                                            if (playerPosY > height - 7)
                                            {
                                                playerPosY = height - 7;
                                            }
                                            else if (playerPosX == bossPosX && playerPosY == bossPosY && bossHp > 0)
                                            {
                                                playerPosY--;
                                            }
                                            else if (playerPosX == princessPosX && playerPosY == princessPosY && bossHp <= 0)
                                            {
                                                playerPosY--;
                                            }
                                            break;

                                        case 'a':
                                        case 'A':
                                            playerPosX -= 2;
                                            if (playerPosX < 2)
                                            {
                                                playerPosX = 2;
                                            }
                                            else if (playerPosX == bossPosX && playerPosY == bossPosY && bossHp > 0)
                                            {
                                                playerPosX += 2;
                                            }
                                            else if (playerPosX == princessPosX && playerPosY == princessPosY && bossHp <= 0)
                                            {
                                                playerPosX+=2;
                                            }
                                            break;

                                        case 'd':
                                        case 'D':
                                            playerPosX += 2;
                                            if (playerPosX > width - 4)
                                            {
                                                playerPosX = width - 4;
                                            }
                                            else if (playerPosX == bossPosX && playerPosY == bossPosY && bossHp > 0)
                                            {
                                                playerPosX -= 2;
                                            }
                                            else if (playerPosX == princessPosX && playerPosY == princessPosY && bossHp <= 0)
                                            {
                                                playerPosX-=2;
                                            }
                                            break;
                                        case 'j':
                                        case 'J':
                                            if ((playerPosX == bossPosX && playerPosY - 1 == bossPosY ||
                                                playerPosX == bossPosX && playerPosY + 1 == bossPosY ||
                                                playerPosX - 2 == bossPosX && playerPosY == bossPosY ||
                                                playerPosX + 2 == bossPosX && playerPosY == bossPosY )&& bossHp > 0)
                                            {
                                                isFighting = true;
                                                Console.SetCursorPosition(2, height - 5);
                                                Console.Write("你已经进入战斗状态");
                                                Console.ForegroundColor = ConsoleColor.Red;
                                                Console.SetCursorPosition(2, height - 4);
                                                Console.Write("按J键继续");
                                            }
                                            else if ((playerPosX == princessPosX && playerPosY - 1 == princessPosY ||
                                                playerPosX == princessPosX && playerPosY + 1 == princessPosY ||
                                                playerPosX - 2 == princessPosX && playerPosY == princessPosY ||
                                                playerPosX + 2 == princessPosX && playerPosY == princessPosY) && bossHp <= 0)
                                            {
                                                isOver = true;
                                            }
                                            break;
                                        default: break;
                                    }
                                    
                                    Console.SetCursorPosition(playerPosX, playerPosY);
                                    Console.ForegroundColor = playerColor;
                                    Console.Write(playerIcon);
                                }
                            }

                            if (isOver)
                            {
                                stateId = 3;
                                endWords = "英雄救美";
                                break;
                            }
                        }

                        break;
                    case 3:
                        Console.SetCursorPosition(width / 2 - 6, 5);
                        Console.Write("游戏结束界面");

                        Console.SetCursorPosition(width / 2 - 4, 7);
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(endWords);

                        int choiceIndex = 0;
                        bool returnMain = false;

                        while (true)
                        {
                            Console.SetCursorPosition(width / 2 - 6, 10);
                            Console.ForegroundColor = choiceIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("返回到主界面");

                            Console.SetCursorPosition(width / 2 - 4, 12);
                            Console.ForegroundColor = choiceIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("退出游戏");

                            char c = Console.ReadKey(true).KeyChar;

                            switch (c)
                            {
                                case 'w':
                                case 'W':
                                    choiceIndex--;
                                    choiceIndex = Math.Max(0, choiceIndex);
                                    break;
                                case 's':
                                case 'S':
                                    choiceIndex++;
                                    choiceIndex = Math.Min(1, choiceIndex);
                                    break;
                                case 'j':
                                case 'J':
                                    if (choiceIndex == 0)
                                    {
                                        stateId = 1;
                                        returnMain = true;
                                    }
                                    else if (choiceIndex == 1)
                                    { 
                                        Environment.Exit(0);
                                    }
                                    break;
                                default:break;
                            }
                        if (returnMain)
                        {
                            break;
                        }
                    }

                    break;
                    default:break;
                }
                
                Console.Clear();
            }
            
        }
    }
}
